package team.lourd.network.system.config;

import lombok.extern.slf4j.Slf4j;
import org.hibernate.cfg.ImprovedNamingStrategy;
import org.hibernate.dialect.H2Dialect;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaDialect;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import team.lourd.network.system.querydsl.ExtendedQuerydslJpaRepositoryFactoryBean;

import javax.annotation.Resource;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "networkSystemEntityManagerFactory",
        transactionManagerRef = "networkSystemTransactionManager",
        basePackages = {"team.lourd.network.system.own.repository.jpa"},repositoryFactoryBeanClass = ExtendedQuerydslJpaRepositoryFactoryBean.class)
@Slf4j
public class NetworkSystemDataSourceConfig {

    @Resource(name = "networkSystemDataSource")
    private DataSource networkSystemDataSource;

    @Value("${spring.jpa.hibernate.primary-dialect}")
    private String primaryDialect;

    @Value("${spring.jpa.hibernate.naming.physical-strategy}")
    private String physicalStrategy;

    public Properties additionalProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", primaryDialect);
        // 配置命名策略
        properties.setProperty(
                "hibernate.physical_naming_strategy", physicalStrategy);

        return properties;
    }

    /**
     * @Description: 注意:和以往配置不一样,必须创建LocalContainerEntityManagerFactoryBean而不是直接创建EntityManagerFactory,
     * 因为除了创建之外,前者还参与异常转换机制EntityManagerFactory
     * @Param: []
     * @return: org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
     * @Author: Lourd
     * @Date: 2019/1/17
     */
    @Primary
    @Bean(name = "networkSystemEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(true);
        vendorAdapter.setDatabase(Database.MYSQL);

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();

        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPersistenceUnitName("mysqlPersistenceUnit");
        factory.setPackagesToScan("team.lourd.network.system.own.domain");
        factory.setDataSource(networkSystemDataSource);
        factory.setJpaProperties(additionalProperties());
        log.info("创建networkSystemEntityManagerFactory成功");
        return factory;
    }

    /**
     * @Description: 配置事务
     * @Param: [entityManagerFactory]
     * @return: org.springframework.transaction.PlatformTransactionManager
     * @Author: Lourd
     * @Date: 2019/1/17
     */
    @Primary
    @Bean(name = "networkSystemTransactionManager")
    public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {

        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory);
        return txManager;
    }

    /**
     * @Description: 返回一个HibernateJpaDialect Bean
     * @Param: []
     * @return: org.springframework.orm.jpa.JpaDialect
     * @Author: Lourd
     * @Date: 2019/1/17
     */
    @Bean
    public JpaDialect jpaDialect() {
        return new HibernateJpaDialect();
    }


}